
#include "../include/server.h"

int main(int argc, const char *argv[])
{
	//判断命令行输入是否正确
	if(3 != argc)
	{
		printf("Usage %s <ip> <port>\n",argv[0]);
		exit(-1);
	}

	//创建客户端套接字进行连接
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sockfd)
	{
		perror("sockfd");
		exit(-1);
	}

	struct sockaddr_in seraddr = {0};
	seraddr.sin_family = AF_INET;
	seraddr.sin_port = htons(atoi(argv[2]));
	seraddr.sin_addr.s_addr = inet_addr(argv[1]);
	int serlen = sizeof seraddr;

	if(-1 == (connect(sockfd,(struct sockaddr *)&seraddr,serlen)))
	{
		perror("connect");
		exit(-1);
	}

	//成功连接后进入交互环节
	char buf[1024] = {0};
	prol_t temp = {0};
	int ret;

	print_usage();
	while(1)
	{
repet3:	ret = recv(sockfd,&temp,sizeof temp,0);
		if(0 == ret)
		{
			printf("end!\n");
			exit(-1);
		}
//	printf("recv: %d\n", ret);
//	printf("recvtype = %d\n",temp.pl_type);
repet1:	switch(temp.pl_type)
		{
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
				send(sockfd,&temp,sizeof temp,0);
//		printf("send:%d,type:%d\n",ret,temp.pl_type);
				break;
			case 10:
				printf("%s%s",temp.info_t.ctldata.ct_arg1,temp.info_t.ctldata.ct_arg2);
repet2:			printf(">>");
				fflush(stdout);
				gets(buf);
				usr_analysis(buf,&temp);//用户输入转换成协议语
//	printf("sendtype = %d\n",temp.pl_type);
				goto repet1;
			case 11:
				printf("%s%s",temp.info_t.ctldata.ct_arg1,temp.info_t.ctldata.ct_arg2);
				goto repet3;
//				client_look();
				break;
			default:
				printf("invalid input!\n");
				goto repet2;
		}
	}
	return 0;
}


